import { Property } from "../../../models/property";
import { Pets } from "../../../models/pets";
import { checkPhone, hint } from "../../../utils/util";
import { formatters } from "../../../utils/util";
import { config } from "../../../config";
const property = new Property();
const pets = new Pets();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollH: "",
    houseDetail: {},
    repairList: [],
    actions: [
      {
        name: "户内",
        typeId: "1",
      },
      {
        name: "公区",
        typeId: "2",
      },
    ],
    isType: false,
    isTime: false,
    active: 0,
    scrollTop: 0,
    type: "",
    commId: "",
    houseId: "",
    appointmentTime: "",
    typeName: "",
    remark: "",
    fileList: [],
    url: "",
    roomNumAll: "",
    commNameShow: false,
    columnsCommName: [],
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      } else if (type === "day") {
        return `${value}日`;
      } else if (type === "hour") {
        return `${value}时`;
      } else if (type === "minute") {
        return `${value}分`;
      }

      return value;
    },
    current: 1,
    pages: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        let height = res.height - 44;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
  },
  initData() {
    property.rentHouseList().then((res) => {
      this.setData({
        houseDetail: res.result[0],
        commId: res.result[0].commid,
        houseId: res.result[0].house_id,
      });
    });

    property.tRepairRecordList().then((res) => {
      this.setData({
        repairList: res.result.records,
        current: res.result.current,
        pages: res.result.pages,
      });
    });

    pets.getHouse().then((res) => {
      let arr = [];
      arr = res.result.map((item, index) => {
        return { text: item.text, value: item.value };
      });
      this.setData({
        columnsCommName: arr,
      });
    });
  },
  handlerGobackClick() {
    wx.navigateTo({
      url: "../property/index",
    });
  },
  selectValue(e) {
    let key = e.target.dataset.key;
    this.setData({
      [key]: true,
    });

    if (key == "isTime") {
      this.setData({
        minDate: new Date().getTime(),
        currentDate: new Date().getTime(),
      });
    }
  },
  selectedType(e) {
    let selectValue = e.detail.name;
    let type = e.detail.typeId;
    let key = e.target.dataset.key;
    this.setData({
      [key]: false,
      type,
      typeName: selectValue,
    });
  },
  commNameConfirm(e) {
    let key = e.target.dataset.key;
    this.setData({
      houseId: e.detail.value.value,
      roomNumAll: e.detail.value.text,
      [key]: false,
    });
  },
  confirm(e) {
    let appointmentTime = formatters(e.detail);
    let key = e.target.dataset.key;
    this.setData({
      appointmentTime,
      [key]: false,
    });
  },
  //取消选择
  cancel(e) {
    let key = e.target.dataset.key;
    this.setData({
      [key]: false,
    });
  },
  onChange(e) {
    this.setData({
      active: e.detail.index,
      scrollTop: 0,
    });
    this.initData();
  },
  afterRead(event) {
    let token = wx.getStorageSync("token");
    const { file } = event.detail;
    wx.uploadFile({
      url: config.baseUrl + "/mini/tRepairRecord/uploadPics", //仅为示例，并非真实的接口地址
      filePath: file.url,
      header: {
        "X-Access-Token": token,
      },
      name: "file",
      success: (res) => {
        let result = JSON.parse(res.data);
        // 上传完成需要更新 fileList
        let { fileList } = this.data;
        fileList.push({ ...file, url: result.message });
        this.setData({ fileList });
      },
    });
  },
  delete(e) {
    let index = e.detail.index;
    let { fileList } = this.data;
    fileList.splice(index, 1);
    this.setData({
      fileList,
    });
  },
  inputChange(e) {
    let key = e.target.dataset.key;
    let value = e.detail;
    this.setData({
      [key]: value,
    });
  },
  detailHandle(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/owner-app/pages/repairDetails/index?id=${id}`,
    });
  },
  submit() {
    let url = "";
    let { commId, houseId, type, appointmentTime, remark, fileList } =
      this.data;
    if (!type) {
      hint("请选择报修类型");
      return;
    }
    if (!appointmentTime) {
      hint("请选择预约时间");
      return;
    }

    if (!remark) {
      hint("请填写报修内容");
      return;
    }
    if (fileList.length) {
      fileList.forEach((v) => {
        url += v.url + ";";
      });
    }

    property
      .tRepairRecordAdd(
        commId,
        houseId,
        type,
        appointmentTime,
        remark,
        url,
        this.data.houseDetail.member_name,
        wx.getStorageSync("user").phone
      )
      .then((res) => {
        hint("提交报修成功");
        this.setData({
          active: 1,
          scrollTop: 0,
          type: "",
          commId: "",
          houseId: "",
          appointmentTime: "",
          typeName: "",
          remark: "",
          fileList: [],
          url: "",
        });
      });
  },
  scrolltolower() {
    let { current, pages, repairList } = this.data;
    current += 1;
    if (current > pages) {
      return;
    }
    property.tRepairRecordList(current).then((res) => {
      repairList.push(...res.result.records);
      this.setData({
        repairList,
        current: res.result.current,
        pages: res.result.pages,
      });
    });
  },
});
